
var	count = 0;
var z = new Array();

var ContactEdit = {
	_cc_option : {
		rules: {
			tel: {
				tel: true
			},
			fax: {
				fax: true
			},
			mobile: {
				mobile: true
			},
			email: {
				email: true
			}
		},
		errorPlacement: function(label, element) {
			label.insertAfter(element);
		},
		success: function(label) {
			label.html("&nbsp;").addClass("checked");
		}
	},
	
	validate_edit: function () {		
		// alert($.toJSON(validator));
		if (!validator.form()) {
			// alert("111");
			return false;
		}
		
		var cc;
		for (var i = 0; i < z.length; i++) {
			cc = $("#form" + z[i]).validate(this._cc_option);	
			if (!cc.form()) {
				// alert("222");
				return false;
			}
		}

		return true;
	},
	
	add: function () {
		z.push(count);

		var contact = $("#t_add").clone();
		contact.attr("id", "contact" + count);

		$("#t_action").attr("id", "t_action_");
		$("#t_form").attr("id", "t_form_");

		contact.insertBefore("#contact_new");
		contact.show();

		$("#t_form").attr("id", "form" + count);
		$("#t_action").attr("id", "action" + count);

		$("#t_form_").attr("id", "t_form");
		$("#t_action_").attr("id", "t_action");

		count++;

		this.refresh();
	},

	remove: function (k) {
		if (!confirm("确认是否删除此联系人？")) {
			return false;
		}
		
		var d = $("#contact" + k + " :input[name='id']").val();

		if (d != null && d.length > 0) {
			var c = this.contact();
			c.push(d);

			$.cookie('contact', $.toJSON(c));
		}

		$("#contact" + k).remove();

		for (var i = 0; i < z.length; i++) {
			if (z[i] == k) {
				z.splice(i, 1);
			}
		}

		this.refresh();
	},

	main: function (k) {
		if (!confirm("确认是否把此联系人设置为主要联系人？")) {
			return false;
		}
		
		var contact = $("#contact" + k).clone();

		contact.attr("id", "contact_temp");
		contact.insertAfter("#info_edit");

		$("#contact" + k).remove();
		contact.attr("id", "contact" + k);

		for (var i = 0; i < z.length; i++) {
			if (z[i] == k) {
				z.splice(i, 1);
				z.splice(0, 0, k);
			}
		}

		this.refresh();
	},

	refresh: function () {
		for (var i = 0; i < z.length; i++) {
			var k = i + 1;
			var legend = "#contact" + z[i] + " legend";
			var action = "#action" + z[i];

			if (i == 0) {
				$(legend).html("主联系人 1");
				$(action).html("");
			} else {
				// $(legend).html("<a href=\"javascript:void(0)\" onclick=\"ContactEdit.remove(" + z[i] + ")\" class=\"ui-icon ui-icon-minus\">x</a>  ");
				$(legend).html("<a href=\"javascript:void(0)\" onclick=\"ContactEdit.remove(" + z[i] + ")\" title=\"删除此联系人\">x</a> 联系人 " + k);
				
				var s = "<a href=\"javascript:void(0)\" onclick=\"ContactEdit.main(" + z[i] + ")\" title=\"设为主联系人\" class=\"ui-icon ui-icon-arrowthickstop-1-n\">设为主联系人</a>";
				s += "<a href=\"javascript:void(0)\" onclick=\"ContactEdit.remove(" + z[i] + ")\" class=\"ui-icon ui-icon-minus\" title=\"删除此联系人\">x</a>  ";
				
				// $(action).html(s);
				
				$(action).html("<a href=\"javascript:void(0)\" onclick=\"ContactEdit.main(" + z[i] + ")\" title=\"设为主联系人\" class=\"ui-icon ui-icon-arrowthickstop-1-n\">设为主联系人</a>");
			}
		}
	},
	
	contact: function () {
		var json = $.cookie('contact');
		if (json != null && json.length > 0) {
			return $.evalJSON(json);
		}

		return new Array();
	}	
};
